<?php
/* @var $this ConfController */
/* @var $model Conf */

$this->breadcrumbs = array(
    'Confs' => array('index'),
    'Holidays',
);

$this->menu = array(
    array('label' => 'List Conf', 'url' => array('index')),
    array('label' => 'Manage Conf', 'url' => array('admin')),
    array('label' => 'Holidays', 'url' => array('holidays')),
);
?>

<h1>Add New Holiday</h1>
<div>
    <form name="form_holiday" id="form_holiday" method="POST" >
        <div style="float:left;width:100%;height:30px;">
            <?php
            $fromDateValue = null;
            echo CHtml::label('Date', 'date', array('class' => 'lable_new'));
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name' => 'date',
                'value' => $fromDateValue,
                'htmlOptions' => array(
                    'id' => 'dateW',
                    'size' => '10', // textField size
                    'maxlength' => '10', // textField maxlength
                    'onchange' => 'onDateChange(this.value)',
                    'class' => 'datePick'
                ),
            ));
            ?>


        </div>
        <div style="float:left;width:100%;height:30px;">
            <div style="float:left;width:150px;">Note :</div><input type="text" name="note"  class="cmn_input ml10" style="width:300px;"/>
        </div>
        <div style="float:left">
            <div style="float:left;width:100%;height:30px;">
                <div style="float:left;">
                    <div style="float:left;width:157px;">
                        All Day :
                    </div>
                    <input type="checkbox" id="allday" name="allday" checked="true" onchange="allDaySelect(this.checked)" /></div>
                <div id="all_day_detail" style="display: none;float:left">
                    Start Time :<input type="text" id="start" name="start" placeholder="HH:MM (24 hour format)" class="cmn_input" style="width:215px;" />
                    End Time :<input type="text" id="end" name="end" placeholder="HH:MM (24 hour format)"  class="cmn_input" style="width:215px;" />
                </div></div>
            <div style="float:left;width:100%;height:30px;">
                <div style="float:left;"> 
                    <div style="float:left;width:157px;">
                        Leave for organization :
                    </div>
                    <input type="checkbox" id="allorg" name="allOrg" checked="true" onchange="empSelect(this.checked)"/></div>
                <div id="emp_detail" style="display: none;float:left">
                    <?php
                    if (isset($resources)) {
                        echo '<select id="employee" name="employee" class="cmn_input_drp"><option value="0">Select Employee</option>';
                        foreach ($resources as $value) {
                            echo '<option value="' . $value->id . '">' . $value->profile->name . '</option>';
                        }
                        echo '</select>';
                    } else {
                        echo 'No Employees Available ';
                    }
                    ?>
                </div></div>
        </div>
        <div style="float:left;width:100%">
            <input type="submit" name="submit" value="Submit" />
        </div>
    </form>

    <script type="text/javascript">
        function getSelectedDate() {
            var d = new Date(document.getElementById('dateW').value);
            var curr_date = d.getDate();
            var curr_month = d.getMonth() + 1; //Months are zero based
            var curr_year = d.getFullYear();
            return(curr_year + "-" + curr_month + "-" + curr_date);

        }
        function  onDelete(id, date) {
            $.get('<?php echo Yii::app()->createUrl('/conf/deleteHoliday') . '?id=' ?>' + id,
                    function(data, status, xhr) {
                        if (status === 'success') {
                            $.get('<?php echo Yii::app()->createUrl('/conf/getHolidaysByDate') . '?date=' ?>' + getSelectedDate(),
                                    function(data, status, xhr) {
                                        document.getElementById('holidayList').innerHTML = data;
                                    });
                        } else {
                            alert(data);
                        }
                    }
            );
        }
        function  onDateChange(date) {
            var s = getSelectedDate();
            $.get('<?php echo Yii::app()->createUrl('/conf/getHolidaysByDate') . '?date=' ?>' + s,
                    function(data, status, xhr) {
                        document.getElementById('holidayList').innerHTML = data;
                    });
        }
    </script>
    <div style="margin-top:20px;float:left;width:100%">
        <h3>Holidays</h3>
        <div id="holidayList">
            <?php
            if (isset($daylist)) {
                echo $daylist;
            }
            ?>
        </div>
    </div>
</div>
<script>
    function  allDaySelect(val) {
        if (val == false) {
            document.getElementById('all_day_detail').style.display = 'block';
        } else {
            document.getElementById('all_day_detail').style.display = 'none';
        }
    }
    function  empSelect(val) {
        if (val == false) {
            document.getElementById('emp_detail').style.display = 'block';
        } else {
            document.getElementById('emp_detail').style.display = 'none';
        }
    }

    $(function() {
        $('form#form_holiday').on('submit', function(e) {
            if (validateForm() !== true) {
                alert('Invalid inputs');
                return false;
            }
            $.ajax({
                type: 'post',
                url: '<?php echo Yii::app()->createUrl('conf/createHoliday'); ?>',
                data: $('form#form_holiday').serialize(),
                success: function() {
                    $.get('<?php echo Yii::app()->createUrl('/conf/getHolidaysByDate') . '?date=' ?>' + date,
                            function(data, status, xhr) {
                                document.getElementById('holidayList').innerHTML = data;
                            });
                }

            });
            e.preventDefault();
            onDateChange(getSelectedDate());
        });
    });
    function validateForm() {
    var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
    var f_date= date_regex.test(document.getElementById('dateW').value);
        if(!f_date){
            alert('Invalid Date');
            return false;
        }
        if (document.getElementById('allday').checked === false) {
            if (validateHhMm(document.getElementById('start').value) !== true) {
                alert("Invalid Start Time");
                return false;
            }
            if (validateHhMm(document.getElementById('end').value) !== true) {
                alert("Invalid End Time");
                return false;
            }
        }
        if (document.getElementById('allorg').value === false) {
            if (document.getElementById('employee').selectedIndex === 0) {
                alert("invalid Selection of Employee");
                return false;
            }
        }
        return  true;
    }
    function validateHhMm(value) {
        var f = /^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$/.test(value);
        return f;
    }

</script>
